/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.stuffrepos.bricknetcenter.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Eduardo H. Bogoni <eduardobogoni@gmail.com>
 */
public class TimeMarker {

    private static class Marker {

        private final String description;
        private final Date date;

        public Marker(String description) {
            this.description = description;
            this.date = new Date();
        }

        public String toString(Marker previous) {
            return String.format("%s: %.3f", description, secondsDiff(previous));
        }

        private double secondsDiff(Marker previous) {
            return ((double) this.date.getTime() - (double) previous.date.getTime()) / (double) 1000;
        }
    }
    private List<Marker> marks = new ArrayList<Marker>();

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();

        for (int i = 1; i < marks.size(); ++i) {
            b.append(marks.get(i).toString(marks.get(i - 1))).append('\n');
        }

        return b.toString();
    }

    public TimeMarker mark(String description) {
        marks.add(new Marker(description));
        return this;
    }
}
